package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Client;
import cn.wolfcode.crm.query.ClientQueryObject;
import cn.wolfcode.crm.query.SellAnalysisQueryObject;
import cn.wolfcode.crm.service.IChartService;
import cn.wolfcode.crm.service.ISellAnalysisService;
import cn.wolfcode.crm.util.JSONUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RequestMapping("sellAnalysis")
@Controller
public class SellAnalysisController {
    @Autowired
    private ISellAnalysisService service;

    @RequestMapping("view")
    public String view() throws Exception {
        return "sellAnalysis";
    }

    //由于dataGrid组件返回必须为json,所以跳转界面就分开

    //页面数据
    @RequestMapping("list")
    @ResponseBody
    public List<Map<String, Object>> list(SellAnalysisQueryObject qo) {
        return service.sellAnalysis(qo);
    }
    //导出
    /*@RequestMapping("exportData")
    public void exportData(HttpServletResponse response, SellAnalysisQueryObject qo) throws IOException {
        //设置响应头
        response.setHeader("Content-Disposition", "attachment;filename=client.xls");
        //创建文件对象
        Workbook wb = new HSSFWorkbook();
        //创建工作簿
        Sheet sheet = wb.createSheet("clientData");
        //查询员工数据(要带高级查询的参数)
        List<?> clientList = service.selectExportData(qo);
        //创建第一行
        Row row = sheet.createRow(0);
        //设置标题
        row.createCell(0).setCellValue("用户名");
        row.createCell(1).setCellValue("真实姓名");
        row.createCell(2).setCellValue("电话");
        row.createCell(3).setCellValue("密码");
        //循环数据
        for (int i = 0; i < clientList.size(); i++) {
            //从第二行开始创建行
            Row row1 = sheet.createRow(i + 1);
            Client client = clientList.get(i);
            //创建单元格
            row1.createCell(0).setCellValue(client.getName());
            row1.createCell(1).setCellValue(client.getLevel());
            row1.createCell(2).setCellValue(client.getTel());
            row1.createCell(3).setCellValue(client.getPassword());
        }

        //输出到单元格
        wb.write(response.getOutputStream());
    }*/
    /*//会员柱状图
    @RequestMapping("view")
    public String saleByBar(@ModelAttribute("qo") SellAnalysisQueryObject qo, Model model){
        List<Object> x = new ArrayList<>();
        List<Object> y = new ArrayList<>();
        //先获取集合里的数据
        List<Map<String,Object>> maps = service.clientChart(qo);
        //然后把集合迭代出来得到map
        for (Map<String, Object> map : maps) {
            //取出当前行中的name和销售金额两个值存入x,y集合中
            x.add(map.get("name"));
            y.add(map.get("number"));
        }
        //转换成json,传递参数
        model.addAttribute("x", JSONUtil.toJSONString(x));
        model.addAttribute("y",JSONUtil.toJSONString(y));
        return "sellAnalysis";
    }*/



}
